<?php
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/common/autoload.php';
include_once '/var/www/html/new/d/class/abc/PFTCoreAPI.class.php';

// if(!isset($_SESSION['sid'])) exit('{"status":"fail","msg":"请先登录后再操作","code":0}');
if ($_REQUEST['auth'] == 'admin') {
	$_SESSION = $_REQUEST['session'];
}
if(!isset($_SESSION['sid'])) exit("<script>alert('登陆过期');window.location='dlogin.html';</script>");


$sid = $_SESSION['sid'];
$did = $_REQUEST['did'];
$pri = $_REQUEST['pri'];

if(count($pri)==0) exit("<script>alert('没有任何配置')</script>");

$pids_a = array_keys($pri);
$pids_s = implode(',', $pids_a);


// 模板绑定
$bind=$_REQUEST["bind"];
$tplid=$_REQUEST["tplid"];
if($bind && $tplid){
	$tpl=Tpl::getLink();
	if(($res=$tpl->apply($did,$tplid))===true) exit("<script>alert('配置成功')</script>");
	exit("<script>alert('".$res."')</script>");
}

// 获取分销商分销商的产品
echo $sql = "select * from pft_product_sale_list where aid=$sid and fid=$did limit 1";
$GLOBALS['le']->query($sql);
if(!$GLOBALS['le']->fetch_assoc()) exit("<script>alert('数据错误')</script>");
$dbPids = $GLOBALS['le']->f('pids');
$dbPids = explode(',', $dbPids);// 获取 pids

// 先去除页面上没有配置的产品
$shows = $_REQUEST['shows'];
if($dbPids[0]!='A'){
	$dbPids = array_diff($dbPids, $shows);
	$dbPids = array_merge($dbPids, array_keys($pri));
}else $dbPids = array_keys($pri);
$dbPids = array_filter($dbPids);
$dbPids = implode(',', $dbPids);

// 获取已配置价格
$sql = "select id,tid,dprice from uu_priceset where pid=$did and aid=0 ";
if($pids_a[0]!='A') $sql.= 'and  tid in ('.$pids_s.')';
$GLOBALS['le']->query($sql);
while($row=$GLOBALS['le']->fetch_assoc()) $prices[$row['tid']] = $row;

$ac      = $GLOBALS['_ac'];
$pw      = $GLOBALS['_pw'];
$inside  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));


// 设置结算价格
$ins_f  = false;
$insert = "insert into uu_priceset (dprice,aid,tid,pid) values ";
foreach($pri as $pid=>$price){
	$pStorage = PFTCoreAPI::pStorage($inside, $_SESSION['saccount'], $pid, $_SESSION['sid'], $today);
	if($pStorage['js']['p']<0) continue;
	if($price<$pStorage['js']['p']) continue;
	$diff = abs($price - $pStorage['js']['p']) * 100;
	if($diff<0) exit("<script>alert('分销价不能小于供货价')</script>");
	if(isset($prices[$pid]) && $prices[$pid]['dprice']==$diff) continue;
	if(isset($prices[$pid])){
		$dbPriId  = $prices[$pid]['id'];
		$sqlBox[] = "update uu_priceset set dprice=$diff where id=$dbPriId and aid=0 and tid=$pid limit 1";
	}else{
		$ins_f  = true;
		$insert.= "($diff,0,$pid,$did),";
	}
}
if($ins_f) $sqlBox[] = rtrim($insert, ',');

// 更新购买权限
$sql = "update pft_product_sale_list set pids='$dbPids' where aid=$sid and fid=$did limit 1";
// exit($sql);
write_logs($sql);
if(!$GLOBALS['le']->query($sql)) exit("<script>alert('权限更新失败')</script>");


// 执行语句
// print_r($sqlBox);
if(count($sqlBox)) foreach($sqlBox as $sql) {
	write_logs($sql);
	$GLOBALS['le']->query($sql);
}
exit("<script>alert('配置成功')</script>");



function write_logs($txt,$file=''){
	if($file=='') $file = '/var/www/html/new/d/abc/logs/priceset_'.$_SESSION['sid'].date('_Y-m-d').'.txt';
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}












